package mq;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

/**
 * 
 * @author zyb
 *
 * 2018年2月21日 下午12:38:20
 */
public class RpcClient {
	
	private DefaultMQProducer producer = new DefaultMQProducer("rpcclient");
	
	private Serializer serializer = new 	ProtostuffSerializer();
	
	public void start() {
		producer.setNamesrvAddr("127.0.0.1:9876");
		try {
			producer.start();
		} catch (MQClientException e) {
			e.printStackTrace();
		}
	}
	
	public <T> ResponseFuture<T> call(String topic, Object... args) throws Exception {
		Request req = Request.create(args);
		ResponseFuture<T> future = new ResponseFuture<>(req, topic);
		RpcContext.addCallback(future);
//		Message msg = new Message("Chat", serializer.serialize(req));
		Message msg = new Message("fuck2", "888888".getBytes(RemotingHelper.DEFAULT_CHARSET));
		try {
			producer.send(msg);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return future;
	}
	
	public static void main(String[] args) throws Exception {
		RpcClient client = new RpcClient();
		client.start();
		client.call("Chat/hello", 666, 888);
	}
	
}
